<?php

if (!defined('DIR_SEP'))
    define('DIR_SEP', DIRECTORY_SEPARATOR);

if (!defined('MODULES_DIR'))
    define('MODULES_DIR', dirname(__FILE__).DIR_SEP."..".DIR_SEP);

if (!defined('PAGES_DIR')) {
    define('PAGES_DIR', MODULES_DIR."..".DIR_SEP."site".DIR_SEP."pages".DIR_SEP);
}

require_once(MODULES_DIR."log.inc");
require_once(MODULES_DIR."config.inc");
require_once(MODULES_DIR."util.inc");

/*
 * Manages all manual/static pages
 */
class PagesLoader
{

	public static function loadPage($pageId)
	{
		if(!isset($pageId) || $pageId == "")
			$pageId = "index";

		$filename = PAGES_DIR.$pageId.".inc";
		
		if ( !file_exists($filename) )
		{
			Log::getInstance()->write("cannot find requested page ".$filename,1);
			return null;
		}
		
		$fp = @fopen($filename, 'r');
		
		if (!$fp)				
			return null;
	
		$xmlFile = simplexml_load_file($filename);
		
		if(!$xmlFile)
			return null;
		
		$content = $xmlFile->xpath("/data/content");
		$title = $xmlFile->xpath("/data/title");
		$desc = $xmlFile->xpath("/data/desc");
		$name = $xmlFile->xpath("/data/name");
		$showInMenu = $xmlFile->xpath("/data/showInMenu");
		$position = $xmlFile->xpath("/data/position");
		if( isset($position[0]) )
			$position = $position[0][0];
		else			
			$position = "0";
		
		$result = array(		
			"content"=> ($content[0][0]), //actual page content.
			"title" => ($title[0][0]), //the browser title
			"desc" => ($desc[0][0]), //the meta description
			"name" => ($name[0][0]), //how it appears in the menu.
			"showInMenu" => ($showInMenu[0][0]), //visible on the main navigation?
			"position" => ($position), //location on the menu
			"filename" => $pageId
		);
		
		return $result;
	}

	public static function savePageInfo($pageId,$pageInfo,$overwrite = true)
	{
		PagesLoader::savePage($pageId,
							$pageInfo['content'],
							$pageInfo['title'],
							$pageInfo['desc'],
							$pageInfo['name'],
							$pageInfo['showInMenu'],
							$pageInfo['position'],
							$overwrite);
	}

	public static function addPage($pageId)
	{
		$arr = PagesLoader::listPages();
		
		$position = count($arr);
		
		return PagesLoader::savePage($pageId,"","","",$pageId,false,$position,false);
	}

	public static function savePage($pageId,$content,$title,$desc,$name,$showInMenu,$position,$overwrite = true)
	{
		if(!isset($pageId) || $pageId == "")
			$pageId = "index";

		$filename = PAGES_DIR.$pageId.".inc";
		
		if( !$overwrite && file_exists($filename))
			return false;
		
		if( !isset($position) )
			$position = "0";
		
		$xml = xmlwriter_open_memory();
		
		if (!$xml)
			return false;

		if (!xmlwriter_write_raw($xml,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>")) return false;
		if (!xmlwriter_start_element($xml,"data")) return false;
		if (!PagesLoader::addSafeElement($xml,"content",$content)) return false;
		if (!PagesLoader::addSafeElement($xml,"title",$title)) return false;
		if (!PagesLoader::addSafeElement($xml,"desc",$desc)) return false;
		if (!PagesLoader::addSafeElement($xml,"name",$name)) return false;
		if (!PagesLoader::addSafeElement($xml,"showInMenu",$showInMenu)) return false;
		if (!PagesLoader::addSafeElement($xml,"position",$position)) return false;
		if (!xmlwriter_end_element($xml)) return false;

		$xmlData =  xmlwriter_output_memory($xml);
		
		$fp = fopen($filename,'w');
		
		if (!$fp)
			return false;
		
		$result = fwrite($fp,$xmlData);
		
		fclose( $fp );
		
		return $result;
	}

	//helper function to add child element to an xml.
	private static function addSafeElement($xml,$name,$content)
	{
		if (!xmlwriter_start_element($xml,$name) ) return false;
		if (!xmlwriter_start_cdata($xml)) return false;
		if (!xmlwriter_text($xml,$content)) return false;
		if (!xmlwriter_end_cdata($xml)) return false;
		if (!xmlwriter_end_element($xml) ) return false;
		
		return true;
	}
	

	public static function listPages()
	{
		$arr = array();

		//list all files
		if ($handle = opendir(PAGES_DIR)) 
		{
			while ($file = readdir($handle))
			{
				if (substr($file, strlen($file)-3, 3)!="inc" ) 
					continue;

				$fileId = substr($file, 0,strlen($file)-4);
				
				$fileInfo = PagesLoader::loadPage($fileId);

				$position = isset($fileInfo['position']) ? $fileInfo['position'] : "0";

				$arr[(string)$position] = $fileInfo;		
			}

			closedir($handle); 
			
			ksort($arr);
		}
		
		return $arr;
	}
		
	public static function delPage($pageId)
	{
		$filename = PAGES_DIR.$pageId.".inc";
		
		if ( !file_exists($filename) )
			return;

		$arr = PagesLoader::listPages();
		
		$found = false;
		foreach($arr as $fileInfo)
		{
			if($found)
			{
				$fileInfo['position'] = $fileInfo['position'] - 1;
				PagesLoader::savePageInfo($fileInfo['filename'],$fileInfo,true);
			}
			else if ($fileInfo['filename'] == $pageId)
				$found = true;			
		}
			
		unlink($filename);
	}
}
?>